using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Presenters;
using Dominio;

public partial class ReleaseListPage : AbstractView
{
    private ReleaseListPresenter presenter;
    public ReleaseListPage()
    {
        presenter = new ReleaseListPresenter();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack) return;
        this.LogedIn();
        stateDrop.DataSource = Enum.GetNames(typeof(ReleaseStatusEnum));
        stateDrop.DataBind();
        stateDrop.Items.Add("All");
        stateDrop.Items.FindByText("All").Selected = true;
        newRelease.Visible = ((User)Session["user"]).Role.isTM((User)Session["user"]);
        releaseList.DataSource = presenter.loadReleaseList(Session["user"]);
        releaseList.DataBind();
    }

    protected void releaseList_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        Response.Redirect("ReleaseDetails.aspx?ReleaseId=" + releaseList.Rows[e.NewSelectedIndex].Cells[6].Text + "&statePage=info");
    }

    protected void releaseList_RowEditing(object sender, GridViewEditEventArgs e)
    {
        Response.Redirect("ReleaseDetails.aspx?ReleaseId=" + releaseList.Rows[e.NewEditIndex].Cells[6].Text + "&statePage=editable");
    }

    protected void newRelease_Click(object sender, EventArgs e)
    {
        Response.Redirect("NewRelease.aspx");
    }

    
    protected void releaseList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("newBug"))
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow selectedRow = ((GridView)e.CommandSource).Rows[index];
            Response.Redirect("NewBug.aspx?releaseId=" + selectedRow.Cells[6].Text);
        }
        
    }
    protected void export_Click(object sender, EventArgs e)
    {
        this.exportToExcel(releaseList);
    }
    protected void reloadLink_Click(object sender, EventArgs e)
    {
        releaseList.DataSource = presenter.loadFilteredReleaseList(fromtxt.Text, totxt.Text, stateDrop.Text, key.Text);
        releaseList.DataBind();
    }
}
